package com.study.rabbitmq.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.integration.mqtt.inbound.MqttPahoMessageDrivenChannelAdapter;
import org.springframework.stereotype.Service;

import java.util.Arrays;

/**
 * mqtt动态添加主题
 */
@Service
public class MqttService {
    @Autowired
    @Qualifier("mqttAdapter")
    private MqttPahoMessageDrivenChannelAdapter adapter;

    @Autowired
    @Qualifier("mqttAdapterTwo")
    private MqttPahoMessageDrivenChannelAdapter adapterTwo;

    public void addTopic(String topic) {
        addTopic(topic, 1);
    }

    public void addTopic(String topic, int qos) {
        String[] topics = adapter.getTopic();
        if (!Arrays.asList(topics).contains(topic)) {
            adapter.addTopic(topic, qos);
        }
        String[] topics2 = adapterTwo.getTopic();
        if (!Arrays.asList(topics2).contains(topic)) {
            adapterTwo.addTopic(topic, qos);
        }
    }

    public void removeTopic(String topic) {
        adapter.removeTopic(topic);
        adapterTwo.removeTopic(topic);
    }
}